package com.Cychat.auth.service;

import com.Cychat.auth.entity.DTO.LoginParamsDTO;
import com.Cychat.auth.mapper.UserMapper;
import com.Cychat.commons.entity.ResultMessage;
import com.Cychat.commons.entity.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

@Service
public class UserService {
    final Logger log = LoggerFactory.getLogger(UserService.class);

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ALiYunOssSevice aLiYunOssSevice;
    /**
     * 根据微信openid查找用户
     * @param openid 微信openid
     * @return 用户对象，如果不存在返回null
     */
    public User findByOpenid(String openid) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", openid);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 创建新用户
     * @param user 用户信息
     * @return 创建后的用户对象
     */
    public User createUser(User user) {
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userMapper.insert(user);
        return user;
    }

    /**
     * 更新用户信息
     *
     * @param userId    用户id
     * @param userDTO 用户信息
     * @return 更新后的用户对象
     */
    public int updateUser(LoginParamsDTO userDTO,Long userId){
        User userput = new User();
        BeanUtils.copyProperties(userDTO, userput);
        userput.setUpdateTime(new Date());
        userput.setUserId(userId);
        int i = userMapper.updateById(userput);
        return i;
    }
    /**
     * 根据用户的userId查询用户信息
     *
     * @param userId 用户id
     * @return User 用户对象
     */
    public User findById(Long userId){
        return userMapper.selectById(userId);
    }
}
